#include "searchHandler.h"


using namespace std;

const string searchHandler::MESSAGE_SEARCH_NO_RESULT = "No matching results have been found";
const string searchHandler::MESSAGE_SEARCH_VECTOR_EMPTY = "Error no entries were found";
const string searchHandler::MESSAGE_INVALID_COMMAND = "Error, command issued is invalid";
const string searchHandler::MESSAGE_INVALID_TIME_FORMAT = "Invalid time/date format: HHMM for time, DD/MM/YYYY for year";
const string searchHandler::MESSAGE_TIME_SLOT_AVAILABLE = "Time slot available";
const string searchHandler::MESSAGE_TIME_SLOT_NOT_AVAILABLE = "Time slot not available";

searchHandler::searchHandler(){
}


// a structure to aid the sort algorithm to sort according to the
//starting time and date of a task
//@author A0097379H
struct timeSort
{
	bool timeSort::operator ()(FloatingTask* task1, FloatingTask* task2) {
		time_t time1 = task1->getStartTimeAndDate();
		time_t time2 = task2->getStartTimeAndDate();
		if (time1 < time2)
			return true;
		else 
			return false;
	}
};
timeSort tS;

vector <FloatingTask*> searchHandler::searchAll(vector<FloatingTask*> &taskList, string parameter){
	vector <int> flag;
	vector <string> parameterVector;
	if (taskList.empty()){
		return taskList;
	}
	bool continueLoop;
	if (parameter.empty() || parameter[CommonConstant::INDEX_ZERO] == CommonConstant::COMMAND_SYMBOL){
		continueLoop = false;
	}	
	else {
		continueLoop = true;
	}
	while (continueLoop){
		parameter = lowerCase(parameter);
		parameterVector.push_back(extractWord(parameter));
		if (parameter.empty() || parameter[CommonConstant::INDEX_ZERO] == CommonConstant::COMMAND_SYMBOL){
			continueLoop = false;
		}	
		else {
			continueLoop = true;
		}
	}
	for (unsigned int i = 0; i < taskList.size(); i++){
		flag.push_back(CommonConstant::TRUE);
	}
	for (unsigned int n = 0; n < parameterVector.size(); n++){
		for (unsigned int i = 0; i < taskList.size(); i++){
			int found = CommonConstant::FALSE;
			found = lowerCase(taskList[i]->getTitle()).find(parameterVector[n]) + CommonConstant::TRUE;
			if (flag[i]){
				if (found){
					flag[i] = CommonConstant::FALSE;
				}
			}
		}
	}
	for (unsigned int n = 0; n < parameterVector.size(); n++){
		for (unsigned int i = 0; i < taskList.size(); i++){
			int found = CommonConstant::FALSE;
			found = lowerCase(taskList[i]->getLocation()).find(parameterVector[n]) + CommonConstant::TRUE;
			if (flag[i]){
				if (found){
					flag[i] = CommonConstant::FALSE;
				}
			}
		}
	}
	for (unsigned int n = 0; n < parameterVector.size(); n++){
		for (unsigned int i = 0; i < taskList.size(); i++){
			int found = CommonConstant::FALSE;
			found = lowerCase(taskList[i]->getPerson()).find(parameterVector[n]) + CommonConstant::TRUE;
			if (flag[i]){
				if (found){
					flag[i] = CommonConstant::FALSE;
				}
			}
		}
	}
	for (unsigned int n = 0; n < parameterVector.size(); n++){
		for (unsigned int i = 0; i < taskList.size(); i++){
			int found = CommonConstant::FALSE;
			found = lowerCase(taskList[i]->getDescription()).find(parameterVector[n]) + CommonConstant::TRUE;
			if (flag[i]){
				if (found){
					flag[i] = CommonConstant::FALSE;
				}
			}
		}
	}
	for (unsigned int n = 0; n < parameterVector.size(); n++){
		for (unsigned int i = 0; i < taskList.size(); i++){
			int found = CommonConstant::FALSE;
			found = lowerCase(taskList[i]->getStringPriority()) == parameterVector[n];
			if (flag[i]){
				if (found){
					flag[i] = CommonConstant::FALSE;
				}
			}
		}
	}
	for (unsigned int n = 0; n < parameterVector.size(); n++){
		for (unsigned int i = 0; i < taskList.size(); i++){
			int found = CommonConstant::FALSE;
			found = lowerCase(taskList[i]->getStringFinishedStatus()) == parameterVector[n];
			if (flag[i]){
				if (found){
					flag[i] = CommonConstant::FALSE;
				}
			}
		}
	}
	for (unsigned int n = 0; n < parameterVector.size(); n++){
		for (unsigned int i = 0; i < taskList.size(); i++){
			if (flag[i]){
				if (taskList[i]->getStartTime() == parameterVector[n] || taskList[i]->getEndTime() == parameterVector[n]){
					flag[i] = CommonConstant::FALSE;
				}
			}
		}
	}
	for (unsigned int n = 0; n < parameterVector.size(); n++){
		for (unsigned int i = 0; i < taskList.size(); i++){
			if (flag[i]){
				if (taskList[i]->getStartDate() == parameterVector[n] || taskList[i]->getEndDate() == parameterVector[n]){
					flag[i] = CommonConstant::FALSE;
				}
			}
		}
	}
	for (unsigned int i = 0; i < flag.size(); i++){
		if (flag[i]){
			taskList.erase(taskList.begin()+i);
			flag.erase(flag.begin()+i);
			i--;
		}
	}
	return taskList;
}

vector<FloatingTask*> searchHandler::searchName(vector<FloatingTask*> &taskList, string parameter){
	vector <int> flag;
	vector <string> parameterVector;
	if (taskList.empty()){
		return taskList;
	}
	bool continueLoop;
	if (parameter.empty() || parameter[CommonConstant::INDEX_ZERO] == CommonConstant::COMMAND_SYMBOL){
		continueLoop = false;
	}	
	else {
		continueLoop = true;
	}
	while (continueLoop){
		parameterVector.push_back(extractWord(parameter));
		if (parameter.empty() || parameter[CommonConstant::INDEX_ZERO] == CommonConstant::COMMAND_SYMBOL){
			continueLoop = false;
		}	
		else {
			continueLoop = true;
		}
	}
	for (unsigned int i = 0; i < taskList.size(); i++){
		flag.push_back(CommonConstant::TRUE);
	}
	while (!parameterVector.empty()){
		for (unsigned int i = 0; i < taskList.size(); i++){
			int found = CommonConstant::FALSE;
			found = taskList[i]->getTitle().find(parameterVector[CommonConstant::INDEX_ZERO]) + CommonConstant::TRUE;
			if (flag[i]){
				if (found){
					flag[i] = CommonConstant::FALSE;
				}
				else {
					flag[i] = CommonConstant::TRUE;
				}
			}
		}
		parameterVector.erase(parameterVector.begin());
	}

	for (unsigned int i = 0; i < flag.size(); i++){
		if (flag[i]){
			taskList.erase(taskList.begin()+i);
			flag.erase(flag.begin()+i);
			i--;
		}
	}
	return taskList;
}

vector<FloatingTask*> searchHandler::searchLocation(vector<FloatingTask*> &taskList, string parameter){
	vector <int> flag;
	vector <string> parameterVector;
	if (taskList.empty()){
		return taskList;
	}
	bool continueLoop;
	if (parameter.empty() || parameter[CommonConstant::INDEX_ZERO] == CommonConstant::COMMAND_SYMBOL){
		continueLoop = false;
	}	
	else {
		continueLoop = true;
	}
	while (continueLoop){
		parameterVector.push_back(extractWord(parameter));
		if (parameter.empty() || parameter[CommonConstant::INDEX_ZERO] == CommonConstant::COMMAND_SYMBOL){
			continueLoop = false;
		}	
		else {
			continueLoop = true;
		}
	}
	flag.clear();
	for (unsigned int i = 0; i < taskList.size(); i++){
		flag.push_back(CommonConstant::TRUE);
	}
	while (!parameterVector.empty()){
		for (unsigned int i = 0; i < taskList.size(); i++){
			int found = CommonConstant::FALSE;
			found = taskList[i]->getLocation().find(parameterVector[CommonConstant::INDEX_ZERO]) + CommonConstant::TRUE;
			if (flag[i]){
				if (found){
					flag[i] = CommonConstant::FALSE;
				}
				else {
					flag[i] = CommonConstant::TRUE;
				}
			}
		}
		parameterVector.erase(parameterVector.begin());
	}
	for (unsigned int i = 0; i < flag.size(); i++){
		if (flag[i]){
			taskList.erase(taskList.begin()+i);
			flag.erase(flag.begin()+i);
			i--;
		}
	}
	return taskList;
}

vector <FloatingTask*> searchHandler::searchPerson(vector<FloatingTask*> &taskList, string parameter){
	vector <int> flag;
	vector <string> parameterVector;
	if (taskList.empty()){
		return taskList;
	}
	bool continueLoop;
	if (parameter.empty() || parameter[CommonConstant::INDEX_ZERO] == CommonConstant::COMMAND_SYMBOL){
		continueLoop = false;
	}	
	else {
		continueLoop = true;
	}
	while (continueLoop){
		parameterVector.push_back(extractWord(parameter));
		if (parameter.empty() || parameter[CommonConstant::INDEX_ZERO] == CommonConstant::COMMAND_SYMBOL){
			continueLoop = false;
		}	
		else {
			continueLoop = true;
		}
	}
	for (unsigned int i = 0; i < taskList.size(); i++){
		flag.push_back(CommonConstant::TRUE);
	}
	while (!parameterVector.empty()){
		for (unsigned int i = 0; i < taskList.size(); i++){
			int found = CommonConstant::FALSE;
			found = taskList[i]->getPerson().find(parameterVector[CommonConstant::INDEX_ZERO]) + CommonConstant::TRUE;
			if (flag[i]){
				if (found){
					flag[i] = CommonConstant::FALSE;
				}
				else {
					flag[i] = CommonConstant::TRUE;
				}
			}
		}
		parameterVector.erase(parameterVector.begin());
	}

	for (unsigned int i = 0; i < flag.size(); i++){
		if (flag[i]){
			taskList.erase(taskList.begin()+i);
			flag.erase(flag.begin()+i);
			i--;
		}
	}
	return taskList;
}

vector <FloatingTask*> searchHandler::searchDescription(vector<FloatingTask*> &taskList, string parameter){
	vector <int> flag;
	vector <string> parameterVector;
	if (taskList.empty()){
		return taskList;
	}
	bool continueLoop;
	if (parameter.empty() || parameter[CommonConstant::INDEX_ZERO] == CommonConstant::COMMAND_SYMBOL){
		continueLoop = false;
	}	
	else {
		continueLoop = true;
	}
	while (continueLoop){
		parameterVector.push_back(extractWord(parameter));
		if (parameter.empty() || parameter[CommonConstant::INDEX_ZERO] == CommonConstant::COMMAND_SYMBOL){
			continueLoop = false;
		}	
		else {
			continueLoop = true;
		}
	}
	for (unsigned int i = 0; i < taskList.size(); i++){
		flag.push_back(CommonConstant::TRUE);
	}
	while (!parameterVector.empty()){
		for (unsigned int i = 0; i < taskList.size(); i++){
			int found = CommonConstant::FALSE;
			found = taskList[i]->getDescription().find(parameterVector[CommonConstant::INDEX_ZERO]) + CommonConstant::TRUE;
			if (flag[i]){
				if (found){
					flag[i] = CommonConstant::FALSE;
				}
				else {
					flag[i] = CommonConstant::TRUE;
				}
			}
		}
		parameterVector.erase(parameterVector.begin());
	}

	for (unsigned int i = 0; i < flag.size(); i++){
		if (flag[i]){
			taskList.erase(taskList.begin()+i);
			flag.erase(flag.begin()+i);
			i--;
		}
	}
	return taskList;
}

vector <FloatingTask*> searchHandler::searchTime(vector<FloatingTask*> &taskList, string parameter){
	vector <int> flag;
	if (taskList.empty()){
		return taskList;
	}
	for (unsigned int i = 0; i < taskList.size(); i++){
		flag.push_back(CommonConstant::FALSE);
	}
	for (unsigned int i = 0; i < taskList.size(); i++){
		if (taskList[i]->getStartTime() == parameter || taskList[i]->getEndTime() == parameter){
			flag[i] = CommonConstant::FALSE;
		}
		else {
			flag[i] = CommonConstant::TRUE;
		}
	}
	for (unsigned int i = 0; i < flag.size(); i++){
		if (flag[i]){
			taskList.erase(taskList.begin()+i);
			flag.erase(flag.begin()+i);
			i--;
		}
	}
	return taskList;
}

vector <FloatingTask*> searchHandler::searchDate(vector<FloatingTask*> &taskList, string parameter){
	vector <int> flag;
	if (taskList.empty()){
		return taskList;
	}
	for (unsigned int i = 0; i < taskList.size(); i++){
		flag.push_back(CommonConstant::FALSE);
	}
	for (unsigned int i = 0; i < taskList.size(); i++){
		if (taskList[i]->getStartDate() == parameter || taskList[i]->getEndDate() == parameter){
			flag[i] = CommonConstant::FALSE;
		}
		else {
			flag[i] = CommonConstant::TRUE;
		}
	}
	for (unsigned int i = 0; i < flag.size(); i++){
		if (flag[i]){
			taskList.erase(taskList.begin()+i);
			flag.erase(flag.begin()+i);
			i--;
		}
	}
	return taskList;
}

//search if the time slot within a time frame specified by the user has already been occupied or not 
string searchHandler::searchAvailableSlot(vector<FloatingTask*> &taskList, string parameter){
	if (taskList.empty()){
		return MESSAGE_TIME_SLOT_AVAILABLE;
	}
	sort(taskList.begin(), taskList.end(), tS);
	time_t begin = CommonConstant::DEFAULT_VALUE;
	time_t end = CommonConstant::INFINITE_TIME;
	string timeString = parameter.substr(CommonConstant::INDEX_ZERO, CommonConstant::MAX_SIZE_24_TIME_STRING_COMMAND_FORMAT_SEARCH);
	string dateString = parameter.substr(CommonConstant::MAX_SIZE_24_TIME_STRING_COMMAND_FORMAT_SEARCH +CommonConstant::INCREMENT, parameter.length() + CommonConstant::DECREMENT);
	int found = CommonConstant::FALSE;
	if (checkTimeClash(timeString, dateString, begin, taskList[CommonConstant::INDEX_ZERO]->getStartTimeAndDate())){
		found = CommonConstant::FALSE;
	}
	else {
		return MESSAGE_TIME_SLOT_AVAILABLE;
	}
	if (checkTimeClash(timeString, dateString, taskList[taskList.size() + CommonConstant::DECREMENT]->getEndTimeAndDate(), end)){	
		found = CommonConstant::FALSE;
	}
	else {
		return MESSAGE_TIME_SLOT_AVAILABLE;
	}
	for (unsigned int i = 0; i < (taskList.size() + CommonConstant::DECREMENT); i++){
		if (checkTimeClash(timeString, dateString, taskList[i]->getEndTimeAndDate(), taskList[i + CommonConstant::INCREMENT]->getStartTimeAndDate())){
			found = CommonConstant::FALSE;
		}
		else {
			return MESSAGE_TIME_SLOT_AVAILABLE;
		}
	}
	if (found == CommonConstant::FALSE){
		return MESSAGE_TIME_SLOT_NOT_AVAILABLE;
	}
	else {
		return MESSAGE_TIME_SLOT_AVAILABLE;
	}
}

vector <FloatingTask*> searchHandler::searchPriority(vector<FloatingTask*> &taskList, string parameter){
	vector <int> flag;
	if (taskList.empty()){
		return taskList;
	}
	for (unsigned int i = 0; i < taskList.size(); i++){
		flag.push_back(CommonConstant::FALSE);
	}
	for (unsigned int i = 0; i < taskList.size(); i++){
		if (lowerCase(taskList[i]->getStringPriority()) == lowerCase(parameter)){
			flag[i] = CommonConstant::FALSE;
		}
		else {
			flag[i] = CommonConstant::TRUE;
		}
	}
	for (unsigned int i = 0; i < flag.size(); i++){
		if (flag[i]){
			taskList.erase(taskList.begin()+i);
			flag.erase(flag.begin()+i);
			i--;
		}
	}
	return taskList;
}

vector <FloatingTask*> searchHandler::searchFinished(vector<FloatingTask*> &taskList, string parameter){
	vector <int> flag;
	if (taskList.empty()){
		return taskList;
	}
	for (unsigned int i = 0; i < taskList.size(); i++){
		flag.push_back(CommonConstant::FALSE);
	}
	for (unsigned int i = 0; i < taskList.size(); i++){

		if (lowerCase(taskList[i]->getStringFinishedStatus()) == lowerCase(parameter)){
			flag[i] = CommonConstant::FALSE;
		}
		else {
			flag[i] = CommonConstant::TRUE;
		}
	}
	for (unsigned int i = 0; i < flag.size(); i++){
		if (flag[i]){
			taskList.erase(taskList.begin()+i);
			flag.erase(flag.begin()+i);
			i--;
		}
	}
	return taskList;
}

searchHandler::COMMAND_TYPE searchHandler::determineCommandType(string command){

	if(command == CommonConstant::KEYWORD_TASK_TITLE)
		return searchHandler::SEARCH_TASK;
	else if(command == CommonConstant::KEYWORD_TASK_LOCATION)
		return searchHandler::SEARCH_LOCATION;
	else if(command == CommonConstant::KEYWORD_TASK_PERSON)
		return searchHandler::SEARCH_PERSON;
	else if(command == CommonConstant::KEYWORD_TASK_DESCRIPTION)
		return searchHandler::SEARCH_DESCRIPTION;
	else if(command == CommonConstant::KEYWORD_SEARCH_TIME)
		return searchHandler::SEARCH_TIME;
	else if(command == CommonConstant::KEYWORD_SEARCH_DATE)
		return searchHandler::SEARCH_DATE;
	else if(command == CommonConstant::KEYWORD_SEARCH_AVAILABILITY)
		return searchHandler::SEARCH_AVAILABLE;
	else if(command == CommonConstant::KEYWORD_TASK_PRIORITY)
		return searchHandler::SEARCH_PRIORITY;
	else if(command == CommonConstant::KEYWORD_TASK_FINISHED_STATUS)
		return searchHandler::SEARCH_FINISHED;
	else
		return searchHandler::INVALID;
}

string searchHandler::executeCommand(string userCommand, vector<FloatingTask*> taskList){
	int found = CommonConstant::FALSE;
	found = userCommand.find(CommonConstant::COMMAND_SYMBOL) + CommonConstant::TRUE;
	vector <FloatingTask*> multiSearchVector = taskList;
	string currentParameter;
	if (found){
		while (found){
			string commandTypeString = extractCommand(userCommand);
			COMMAND_TYPE commandType = determineCommandType(commandTypeString);
			if (userCommand == commandTypeString){
				return MESSAGE_INVALID_COMMAND;
			}
			if (userCommand[commandTypeString.length() + CommonConstant::INCREMENT] == CommonConstant::COMMAND_SYMBOL){
				return MESSAGE_INVALID_COMMAND;
			}
			checkStandardCommand(userCommand, commandTypeString, currentParameter, found);
			if (checkDateCommand(userCommand, commandTypeString, currentParameter, found)){
				return MESSAGE_INVALID_COMMAND;
			}
			if (checkAvailabilityCommand(userCommand, commandTypeString, currentParameter, found)){
				return MESSAGE_INVALID_COMMAND;
			}
			switch (commandType)
			{
			case searchHandler::SEARCH_TASK:
				searchName(multiSearchVector, currentParameter);
				break;
			case searchHandler::SEARCH_LOCATION:
				searchLocation(multiSearchVector, currentParameter);
				break;
			case searchHandler::SEARCH_PERSON:
				searchPerson(multiSearchVector, currentParameter);
				break;
			case searchHandler::SEARCH_DESCRIPTION:
				searchDescription(multiSearchVector, currentParameter);
				break;
			case searchHandler::SEARCH_TIME:
				searchTime(multiSearchVector, currentParameter);
				break;
			case searchHandler::SEARCH_DATE:
				searchDate(multiSearchVector, currentParameter);
				break;
			case searchHandler::SEARCH_AVAILABLE:
				return searchAvailableSlot(multiSearchVector, currentParameter);
				break;
			case searchHandler::SEARCH_PRIORITY:
				searchPriority(multiSearchVector, currentParameter);
				break;
			case searchHandler::SEARCH_FINISHED:
				searchFinished(multiSearchVector, currentParameter);
				break;
			case searchHandler::INVALID:
				return MESSAGE_INVALID_COMMAND;
				break;
			default:
				throw new exception("unrecongnized command type");
			}
		}
	}
	else {
		searchAll(multiSearchVector, userCommand);
	}
	string searchResults;
	if (multiSearchVector.empty()){
		searchResults = MESSAGE_SEARCH_NO_RESULT;
	}
	else{
		for (unsigned int i = 0; i < multiSearchVector.size(); i++){
			searchResults += to_string(i+1) + CommonConstant::END_LINE + multiSearchVector[i]->taskInfomation() + CommonConstant::END_LINE;
		}
	}
	return searchResults;

}

void searchHandler::checkStandardCommand(string& userCommand, string& commandTypeString, string& currentParameter, int& found){
	bool isStandardCommand;
	if (commandTypeString == CommonConstant::KEYWORD_SEARCH_DATE || commandTypeString == CommonConstant::KEYWORD_SEARCH_AVAILABILITY){
		isStandardCommand = false;
	}
	else {
		isStandardCommand = true;
	}
	if (isStandardCommand){
		currentParameter = userCommand.substr(commandTypeString.length() + CommonConstant::INCREMENT, userCommand.length() + CommonConstant::DECREMENT);
		found = CommonConstant::FALSE;
		int nextCommandIterator = CommonConstant::DECREMENT;
		nextCommandIterator = currentParameter.find(CommonConstant::COMMAND_SYMBOL) + CommonConstant::INCREMENT;
		if (nextCommandIterator){
			found = CommonConstant::TRUE;
			nextCommandIterator += CommonConstant::DECREMENT;
			userCommand = currentParameter.substr(nextCommandIterator, currentParameter.length() + CommonConstant::DECREMENT);
			currentParameter = currentParameter.substr(commandTypeString.length() + CommonConstant::INCREMENT, nextCommandIterator);
		}
	}
}

bool searchHandler::checkDateCommand(string& userCommand, string& commandTypeString, string& currentParameter, int& found){
	if (commandTypeString == CommonConstant::KEYWORD_SEARCH_DATE){
		if (userCommand.length() == 14){
			currentParameter = userCommand.substr(CommonConstant::MAX_SIZE_24_TIME_STRING_FORMAT, 14);;
			userCommand.clear();
			found = CommonConstant::FALSE;
			return false;
		}
		else if (userCommand.length() >= 14){
			currentParameter = userCommand.substr(CommonConstant::MAX_SIZE_24_TIME_STRING_FORMAT, 14);
			userCommand = userCommand.substr(15, userCommand.length() - 1);
			return false;
		}
		else {
			return true;
		}
	}
	return false;
}

bool searchHandler::checkAvailabilityCommand (string& userCommand, string& commandTypeString, string& currentParameter, int& found){
	if (commandTypeString == CommonConstant::KEYWORD_SEARCH_AVAILABILITY){
		if (userCommand.length() == 34){
			currentParameter = userCommand.substr(3, 31);;
			userCommand.clear();
			found = 0;
			return false;
		}
		else if (userCommand.length() >= 34){
			currentParameter = userCommand.substr(3, 34);
			userCommand = userCommand.substr(35, userCommand.length() - 1);
			return false;
		}
		else {
			return true;
		}
	}
	return false;
}

int searchHandler::convertStringToInt(string parameter){
	return atoi(parameter.c_str());
}

string searchHandler::lowerCase(string parameter){
	for (unsigned int i = 0; i < parameter.length(); i++){
		parameter[i] = tolower(parameter[i]);
	}
	return parameter;
}

string searchHandler::extractCommand(string usercommand){
	if (usercommand[CommonConstant::INDEX_ZERO] == CommonConstant::COMMAND_SYMBOL){
		istringstream iss(usercommand);
		string command_type;
		iss >> command_type;
		return command_type;
	}
	else {
		return CommonConstant::KEYWORD_TASK_ALL;
	}
}

string searchHandler::extractWord(string &parameter){
	istringstream iss(parameter);
	string word;
	iss >> word;
	if (parameter == word){
		parameter.clear();
		return word;
	}
	bool isNotLastWord;
	if  (parameter[CommonConstant::INDEX_ZERO] == CommonConstant::COMMAND_SYMBOL || parameter.length() == word.length()){
		isNotLastWord = false;
	}
	else {
		isNotLastWord = true;
	}
	if (isNotLastWord){ 
		parameter = parameter.substr(word.length() + CommonConstant::INCREMENT, parameter.length() + CommonConstant::DECREMENT);
	}
	return word;
}

//checks if a time clash occurs within the time fram specified by the user and the different tasks in system 
bool searchHandler::checkTimeClash(string timeParameter, string dateParameter, time_t start, time_t end){
	struct tm timer1;
	struct tm timer2;
	int hour1, minute1, hour2, minute2;
	time_t userTime1;
	time_t userTime2;
	time(&userTime1);
	time(&userTime1);
	string time1 = timeParameter.substr(CommonConstant::INDEX_ZERO, CommonConstant::MAX_SIZE_24_TIME_STRING_FORMAT);
	string time2 = timeParameter.substr(time1.length() + CommonConstant::INCREMENT, timeParameter.length() + CommonConstant::DECREMENT);
	sscanf_s(time1.c_str(), "%2d%2d",&hour1,&minute1);
	timer1.tm_min=minute1;
	timer1.tm_hour=hour1;
	timer1.tm_sec = CommonConstant::DEFAULT_TIME_SECONDS;
	sscanf_s(time2.c_str(), "%2d%2d",&hour2,&minute2);
	timer2.tm_min=minute2;
	timer2.tm_hour=hour2;
	timer2.tm_sec = CommonConstant::DEFAULT_TIME_SECONDS;
	int day1, month1, year1, day2, month2, year2;
	string date1 = dateParameter.substr(CommonConstant::INDEX_ZERO, CommonConstant::SIZE_DATE_STRING_FORMAT);
	string date2 = dateParameter.substr(date1.length() + CommonConstant::INCREMENT, dateParameter.length() + CommonConstant::DECREMENT);
	sscanf_s(date1.c_str(), "%2d/%2d/%4d",&day1,&month1,&year1);
	timer1.tm_mday = day1;
	timer1.tm_mon = month1 + CommonConstant::DECREMENT;
	timer1.tm_year = year1 - CommonConstant::BASE_YEAR;
	userTime1 = mktime(&timer1);
	sscanf_s(date2.c_str(), "%2d/%2d/%4d",&day2,&month2,&year2);
	timer2.tm_mday = day2;
	timer2.tm_mon = month2 + CommonConstant::DECREMENT;
	timer2.tm_year = year2 - CommonConstant::BASE_YEAR;
	userTime2 = mktime(&timer2);

	if (userTime1 <= start || userTime2 >= end)
		return true;
	else 
		return false;
}

